<template>
	<page-meta :page-style="show ? 'overflow: hidden;' : '' " id="details">
		<view>
			<!-- 主要内容 -->
			<van-skeleton title row="3" :loading='loading'>
				<!-- 头部-->
				<view class="header">
					<!-- 标题 -->
					<text class="title">{{detail.title}}</text>
					<!-- 时间、阅读、评论 -->
					<view class="info">
						<view>
							{{detail.pubdate}}
						</view>
						<view>
							{{detail.read_count}}阅读
						</view>
						<view>
							{{detail.comm_count}}评论
						</view>
					</view>
					<van-nav-bar>
						<van-button type="primary" size="small" slot="right"
							:color='detail.is_followed ? "#f7f8fa" : "#fc6627" '
							:custom-style="detail.is_followed ? 'color:#a5a6ab' : 'color:#fff'" round @click='followed'>
							{{
							detail.is_followed ? '已关注' : "关注"
						}}
						</van-button>
						<view class=" aut" slot='left'>
							<van-image width="40" height="40" :src="detail.aut_photo" round />
							<text>{{detail.aut_name}}</text>
						</view>
					</van-nav-bar>
				</view>
				<view class="contetn">
					<towxml :nodes="contents" />
				</view>
				<!-- 底部评论区 -->
				<view class="comment-but">
					<view class="con">
						<view class="con-header">
							<text>全部评论（0）</text>
							<text class="discuss">0点赞</text>
						</view>
						<van-empty image="error" description="描述文字" />
					</view>
				</view>
			</van-skeleton>
			<!-- 底部导航栏 -->
			<van-tabbar @change='tabChange' active-color="#fc6627" inactive-color="#000">
				<van-tabbar-item>
					<view class="comment">
						<van-image width="20" height="20" src="/static/写字.png" />
						<text>抢沙发</text>
					</view>
				</van-tabbar-item>
				<van-tabbar-item icon="chat-o">评论</van-tabbar-item>
				<van-tabbar-item>
					<image slot="icon" src="/static/点赞.png" mode="aspectFit" style="width: 35px; height: 22px;" />
					<image slot="icon-active" src="/static/点赞 (1).png" mode="aspectFit"
						style="width: 35px; height: 22px;" />
					点赞
				</van-tabbar-item>
				<van-tabbar-item>
					<image slot="icon" src="/static/收藏.png" mode="aspectFit" style="width: 25px; height: 20px;" />
					<image slot="icon-active" src="/static/收藏 (1).png" mode="aspectFit"
						style="width: 35px; height: 22px;" />
					收藏
				</van-tabbar-item>
				<van-tabbar-item>
					<image slot="icon" src="/static/3.1分享.png" mode="aspectFit" style="width: 35px; height: 22px;" />
					<image slot="icon-active" src="/static/3.1分享 (1).png" mode="aspectFit"
						style="width: 35px; height: 22px;" />
					分享
				</van-tabbar-item>
			</van-tabbar>
			<!-- 发表评论弹出层 -->
			<van-popup :show="show" position="bottom" custom-style="height: 100%;">
				<view class="showheader">
					<van-button size="small" type="primary" color='#fc6627' plain @click='release'>发表</van-button>
					<van-button plain type="primary" size="small" color='#fc6627' @click='cancel'>取消</van-button>
				</view>
				<!-- 评论输入框 -->
				<view class="text">
					<textarea v-model="comment" placeholder="说点什么~" />
				</view>
			</van-popup>
		</view>
	</page-meta>
</template>

<script setup>
	import {
		onLoad,
		onReady,
		onPageScroll,
		onUnload
	} from '@dcloudio/uni-app'
	import {
		onMounted,
		ref,
		getCurrentInstance,
		reactive
	} from "vue";
	import req from '@/api/index.js'
	const {
		proxy
	} = getCurrentInstance()
	const loading = ref(true)
	// 评论内容
	const comment = ref('');
	// 评论弹出层控制
	const show = ref(false)
	// 底部tab
	const tabChange = (e) => {
		if (e.detail == 0) {
			// console.log(23);
			show.value = true
		} else if (e.detail == 1) {
			jump()
		}
	}
	//文章详情
	const detail = ref({})
	const contents = ref({})
	onLoad(async (e) => {
		// console.log(document.querySelector('.header'));
		let {
			data
		} = await req(`http://toutiao.itheima.net/v1_0/articles/${e.art_id}`)
		data.data.pubdate = data.data.pubdate.split(' ')[0]
		detail.value = data.data
		parseMarkdown(data.data.content)
		loading.value = false
		commentquery.source = e.art_id
		// console.log(commentquery);
		getcomment()
		// if (commentquery.source) {}
	})
	const parseMarkdown = (content) => {
		// console.log(122);
		let result = proxy.$towxml(content,
			'markdown', {
				// base: 'https://xxx.com', // 相对资源的base路径
				// theme: 'dark', // 主题，默认`light`
				events: { // 为元素绑定的事件方法
					tap: (e) => {
						console.log('tap', e);
					}
				}
			});
		// 更新解析数据
		contents.value = result
		// console.log(result);

	}
	// 关注与取关
	const followed = async () => {
		// console.log(detail.value.is_followed);
		let list = {};
		let status = ''
		if (detail.value.is_followed) {
			// 取关
			let {
				data,
				statusCode
			} = await req(`http://toutiao.itheima.net/v1_0/user/followings/${detail.value.aut_id}`, "DELETE")
			list = data
			status = statusCode
		} else {
			// 关注
			let {
				data,
				statusCode
			} = await req('http://toutiao.itheima.net/v1_0/user/followings', 'POST', data = {
				target: detail.value.aut_id
			})
			list = data
			status = statusCode
		}
		if (status == 401) {
			uni.navigateTo({
				url: '/pages/Login/Login'
			})
			return
		}
		if (status == 204 || status == 201) {
			detail.value.is_followed = !detail.value.is_followed
		}
	}
	onPageScroll((res) => {
		let query = uni.createSelectorQuery().select('.contetn');
		query.boundingClientRect(data => {
			if (data.top <= 1) {
				uni.setNavigationBarTitle({
					title: detail.value.aut_name
				})
			} else {
				uni.setNavigationBarTitle({
					title: ''
				})
			}
		}).exec()
	})
	// 发布评论
	const release = () => {
		console.log(comment.value);
	};
	// 取消发布
	const cancel = () => {
		show.value = false
		comment.value = ''
	};
	// 定位到评论区域
	const jump = () => {
		console.log(123);
		uni.createSelectorQuery().select('.comment-but').boundingClientRect((con) => {
			uni.createSelectorQuery().select('#details').boundingClientRect((res) => {
				uni.pageScrollTo({
					selector: '#details',
					duration: 800,
					scrollTop: con.top - res.top
				})
			}).exec()
		}).exec()
	}

	// 评论的参数
	const commentquery = reactive({
		type: 'a',
		source: ''
	})
	// 获取文章评论内容
	const getcomment = async () => {
		let data = await uni.request({
			url: 'http://toutiao.itheima.net/v1_0/comments',
			method: "GET",
			data: commentquery
		})
		console.log(data);
	}
</script>

<style lang="less">
	#details {
		width: 100%;
		height: 100vh;
		overflow-y: auto;

		.header {

			padding: 0 15px;
			padding-top: 15px;
			background-color: #fff;

			.title {
				font-size: 20px;
				font-weight: bold;
				margin-bottom: 16px;
				display: block;
			}

			.info {
				display: flex;
				color: #bfc2cb;

				view {
					font-size: 12px;
					padding: 0 9px;
				}

				view:nth-child(1) {
					padding-left: 0;
				}

				view:nth-child(2) {
					border-left: 1.5px solid #bfc2cb;
					border-right: 1.5px solid #bfc2cb;
				}
			}

			.van-nav-bar__left {
				left: 0;
			}

			.van-nav-bar__right {
				right: 0;
			}

			.van-hairline--bottom:after {
				border-bottom-color: #dee1eb;
			}

			.aut {
				display: flex;

				text {
					margin-left: 12px;
				}

			}
		}

		van-skeleton {
			margin-top: 12px;
		}

		.comment-but {
			padding-top: 10px;
			background-color: #f7f8fa;

			.con {
				background-color: #fff;

				.con-header {
					display: flex;
					justify-content: space-between;
					padding: 10px;

					.discuss {
						font-size: 14px;
						color: #d4d5d6;
					}
				}
			}

		}

		van-tabbar-item:nth-child(1) .van-tabbar-item {
			width: 49vw;
			align-items: initial;
			padding: 0 14px;

			.comment {
				// width: 40%;
				background-color: #f7f8fa;
				display: flex;
				width: 90%;
				padding: 6px 0;
				border-radius: 50px;
				align-items: center;
				padding-left: 15px;
				font-size: 12px;

				text {
					margin-left: 5px;
				}
			}
		}

		.showheader {
			text-align: right;
			padding: 20px;

			van-button {
				margin-left: 12px;
			}
		}

		.text textarea {
			padding: 5px;
		}
	}
</style>
